package main.java.easy;


/**
 * @Author: 郑国荣
 * @Date: 2023-07-21-12:37
 * @Description: 移除元素
 * 双指针，反向思维
 * 时间复杂度O(n),空间复杂度O(1)
 */
public class RemoveElement {
    public static int removeElement(int[] nums, int val) {
        int n = nums.length;
        int left=0,right=0;
        while (right<n){
            if (nums[right]!=val){
                nums[left] = nums[right];
                left++;
            }
            right++;
        }
        return left;
    }

    public static void main(String[] args) {
        int[] a = {0,1,3,2,3,2,2,2,2};
        int len = removeElement(a, 2);
        System.out.println("len:"+len);
        for (int i = 0; i < len; i++) {
            System.out.println(a[i]);
        }
    }
}
